import { Controller, Get, Param, Query } from '@nestjs/common';
import { CalculatePowerTowerService } from 'src/calculate/calculate-power-tower.service';
import { CalculatePowerAcrossService } from './calculate-power-across.service';
import { CalculatePowerConfigService } from './calculate-power-config.service';
import { config } from 'process';
import { find } from 'lodash';

@Controller('calculate-power')
export class CalculatePowerController {
  constructor(
    private readonly calculatePowerConfig: CalculatePowerConfigService,
    private readonly calculatePowerAcross: CalculatePowerAcrossService,
    private readonly calculatePowerTower: CalculatePowerTowerService,
  ) {}

  @Get('calculate')
  async calculatePower(@Query() config: Record<string, any> = {}) {
    /**
     * !有没有必要保存bet的计算值
     * *我认为是没有必要的
     * *TT--张力整定值
     * */
    const { id, zoneId } = await this.calculatePowerConfig.calculate(config);
    const { TT } = await this.calculatePowerAcross.calculate({
      configId: id,
      zoneId,
    });
    await this.calculatePowerTower.calculate(
      {
        configId: id,
        zoneId,
      },
      TT,
    );

    /**
     * !将张力整定值存入config中
     */
    await this.calculatePowerConfig.updadte({ TT }, id);

    return '计算成功';
  }

  @Get('all')
  async findAll(@Query() args: Record<string, any>) {
    return await this.calculatePowerConfig.findAll(args);
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    return {
      config: await this.calculatePowerConfig.find(+id),
      powerAcross: await this.calculatePowerAcross.findAll({ configId: +id }),
      powerTowers: await this.calculatePowerTower.findAll({ configId: +id }),
    };
  }
}
